package com.ok.api.config;

import com.ok.api.client.fallback.IntegralFallback;
import com.ok.api.client.fallback.PrizeFallback;
import com.ok.api.client.fallback.UserFallback;
import com.ok.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {

    @Bean   //定义feign的日志级别
    public Logger.Level feignLogLevel() {
        return Logger.Level.FULL;
    }

    @Bean
    public RequestInterceptor userInfoRequestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                // 获取登录用户
                Long userId = UserContext.getUser();
                if (userId == null) {
                    // 如果为空则直接跳过
                    return;
                }
                // 如果不为空则放入请求头中，传递给下游微服务
                template.header("user-info", userId.toString());


            }
        };
    }

    //注册Fallback对象
    @Bean
    public IntegralFallback integralFallback() {
        return new IntegralFallback();
    }
    @Bean
    public UserFallback userFallback(){
        return new UserFallback();
    }
    @Bean
    public PrizeFallback PrizeFallback(){
        return new PrizeFallback();
    }

}
